<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
    >
      <commentItem @click-reply="$emit('click-reply',$event)" v-for="item in list" :row="item" :key="item.com_id+''" />
    </van-list>
  </div>
</template>

<script>
import { getCommentList } from '@/api/article.js'
import commentItem from './comment-item.vue'
export default {
  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Number, Object, String],
      require: true
    },
    list: {
      type: Array,
      require: true
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },

  created () {
    this.onLoad()
  },

  methods: {
    async onLoad () {
      const res = await getCommentList({
        type: this.type,
        source: this.source + '',
        offset: this.offset,
        limit: this.limit
      })
      this.$emit('updateTotal', res.total_count)
      this.list.push(...res.results)
      this.loading = false
      if (!res.last_id || this.list.length >= res.total_count) {
        this.finished = true
      } else {
        this.offset = res.last_id
      }
    }
  },
  components: {
    commentItem
  }
}
</script>

<style scoped>

</style>
